using System;
using Newtonsoft.Json;
using System.IO;

namespace LarkShot
{
    public class AppConfig
    {
        public string HotKey { get; set; } = "PrintScreen";
        public bool AutoCrop { get; set; } = true;
        public bool ShowToastNotification { get; set; } = true;
        public string ScreenshotPath { get; set; } = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "screenshots");
        
        // 硬编码目标窗口，不可更改
        public string TargetWindowTitle => "GateMK-Win64-Shipping";

        private static readonly string ConfigPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "config.json");

        public static AppConfig Load()
        {
            try
            {
                if (File.Exists(ConfigPath))
                {
                    var json = File.ReadAllText(ConfigPath);
                    return JsonConvert.DeserializeObject<AppConfig>(json) ?? new AppConfig();
                }
            }
            catch (Exception ex)
            {
                System.Windows.MessageBox.Show($"配置文件加载失败: {ex.Message}", "警告");
            }
            return new AppConfig();
        }

        public void Save()
        {
            try
            {
                var json = JsonConvert.SerializeObject(this, Formatting.Indented);
                File.WriteAllText(ConfigPath, json);
            }
            catch (Exception ex)
            {
                System.Windows.MessageBox.Show($"配置文件保存失败: {ex.Message}", "错误");
            }
        }

        public void EnsureScreenshotDirectory()
        {
            if (!Directory.Exists(ScreenshotPath))
            {
                Directory.CreateDirectory(ScreenshotPath);
            }
        }
    }
}
